package com.caifeng.myapplication;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import java.util.ArrayList;
import java.util.List;

public class TwoFragment extends Fragment {

    private View view;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.two, null);

        initView();
        initListener();
        return view;
    }

    private TextView one, two, three, four, five, six;
    private ViewPager viewPager;

    /**
     * 好友 分组 设备 通讯录 公众号 群聊  共6个
     */
    private void initView() {
        one = view.findViewById(R.id.friends);
        two = view.findViewById(R.id.fenzu);
        three = view.findViewById(R.id.device);
        four = view.findViewById(R.id.address);
        five = view.findViewById(R.id.tencent);
        six = view.findViewById(R.id.groupchat);


        viewPager = view.findViewById(R.id.vp);
        viewPager.setAdapter(new FaAdapter(getChildFragmentManager(), getFragment()));
        viewPager.addOnPageChangeListener(onPageChangeListener);
    }


    private void initListener() {
        one.setOnClickListener(onClickListener);
        two.setOnClickListener(onClickListener);
        three.setOnClickListener(onClickListener);
        four.setOnClickListener(onClickListener);
        five.setOnClickListener(onClickListener);
        six.setOnClickListener(onClickListener);
    }

    private List<Fragment> getFragment() {
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new Vfriend());
        fragments.add(new Vfenzu());
        fragments.add(new Vgroupchat());
        fragments.add(new Vdevice());
        fragments.add(new Vaddress());
        fragments.add(new Vtencent());


        return fragments;
    }

    /**
     * TextView变色效果
     */
    private void setTestData() {
        one.setTextColor(getResources().getColor(R.color.grey));
        two.setTextColor(getResources().getColor(R.color.grey));
        three.setTextColor(getResources().getColor(R.color.grey));
        four.setTextColor(getResources().getColor(R.color.grey));
        five.setTextColor(getResources().getColor(R.color.grey));
        six.setTextColor(getResources().getColor(R.color.grey));

        one.setBackgroundColor(getResources().getColor(R.color.white));
        two.setBackgroundColor(getResources().getColor(R.color.white));
        three.setBackgroundColor(getResources().getColor(R.color.white));
        four.setBackgroundColor(getResources().getColor(R.color.white));
        five.setBackgroundColor(getResources().getColor(R.color.white));
        six.setBackgroundColor(getResources().getColor(R.color.white));
    }

    //
    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.friends:
                    viewPager.setCurrentItem(0);
//                    setTestData 选中没选中 字体变色
                    setTestData();
                    one.setTextColor(getResources().getColor(R.color.black));
                    one.setBackground(getResources().getDrawable(R.drawable.two_text));
                    viewPager = view.findViewById(R.id.vp);
                    break;
                case R.id.fenzu:
                    viewPager.setCurrentItem(1);
//                    setTestData 选中没选中 字体变色
                    setTestData();
                    two.setTextColor(getResources().getColor(R.color.black));
                    two.setBackground(getResources().getDrawable(R.drawable.two_text));
                    viewPager = view.findViewById(R.id.vp);
                    break;
                case R.id.device:
                    viewPager.setCurrentItem(2);
//                    setTestData 选中没选中 字体变色
                    setTestData();
                    three.setTextColor(getResources().getColor(R.color.black));
                    three.setBackground(getResources().getDrawable(R.drawable.two_text));
                    viewPager = view.findViewById(R.id.vp);
                    break;
                case R.id.address:
                    viewPager.setCurrentItem(3);
//                    setTestData 选中没选中 字体变色
                    setTestData();
                    four.setTextColor(getResources().getColor(R.color.black));
                    four.setBackground(getResources().getDrawable(R.drawable.two_text));
                    viewPager = view.findViewById(R.id.vp);
                    break;
                case R.id.tencent:
                    viewPager.setCurrentItem(4);
//                    setTestData 选中没选中 字体变色
                    setTestData();
                    five.setTextColor(getResources().getColor(R.color.black));
                    five.setBackground(getResources().getDrawable(R.drawable.two_text));
                    viewPager = view.findViewById(R.id.vp);
                    break;
                case R.id.groupchat:
                    viewPager.setCurrentItem(5);
//                    setTestData 选中没选中 字体变色
                    setTestData();
                    six.setTextColor(getResources().getColor(R.color.black));
                    six.setBackground(getResources().getDrawable(R.drawable.two_text));
                    viewPager = view.findViewById(R.id.vp);
                    break;

            }

        }
    };
    /**
     * PagerChange
     */
    private ViewPager.OnPageChangeListener onPageChangeListener =
            new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {

                    switch (position) {
                        case 0:
                            viewPager.setCurrentItem(0);
                            setTestData();
//                            one.setText();
                            one.setBackground(getResources().getDrawable(R.drawable.two_text));
                            one.setTextColor(getResources().getColor(R.color.black));
                            break;
                        case 1:
                            viewPager.setCurrentItem(1);
                            setTestData();
//                            one.setText();
                            two.setBackground(getResources().getDrawable(R.drawable.two_text));
                            two.setTextColor(getResources().getColor(R.color.black));
                            break;
                        case 2:
                            viewPager.setCurrentItem(2);
                            setTestData();
//                            one.setText();
                            three.setBackground(getResources().getDrawable(R.drawable.two_text));
                            three.setTextColor(getResources().getColor(R.color.black));
                            break;
                        case 3:
                            viewPager.setCurrentItem(3);
                            setTestData();
//                            one.setText();
                            four.setBackground(getResources().getDrawable(R.drawable.two_text));
                            four.setTextColor(getResources().getColor(R.color.black));
                            break;
                        case 4:
                            viewPager.setCurrentItem(4);
                            setTestData();
//                            one.setText();
                            five.setBackground(getResources().getDrawable(R.drawable.two_text));
                            five.setTextColor(getResources().getColor(R.color.black));
                            break;
                        case 5:
                            viewPager.setCurrentItem(5);
                            setTestData();
//                            one.setText();
                            six.setBackground(getResources().getDrawable(R.drawable.two_text));
                            six.setTextColor(getResources().getColor(R.color.black));
                            break;
                    }
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            };


}
